package com.admin4j.server;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.fill.Column;
import org.apache.commons.lang3.StringUtils;

import java.sql.Types;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author andanyang
 * @since 2023/5/29 15:20
 */
public class GeneratorUIServer {

    private final static String DB_URL = "jdbc:mysql://192.168.0.252:3306/admin4j?remarks=true&useInformationSchema=true";
    private final static String DB_USERNAME = "root";
    private final static String DB_PASSWORD = "TNjddIqK.1";

    public static void main(String[] args) {

        // 包名
        String packageName = GeneratorUIServer.class.getPackage().getName();
        // 当前路径
        String userDir = System.getProperty("user.dir");
        String javaPath = userDir + "/src/main/java";
        String xmlPath = userDir + "/src/main/resources/mapper/";

        FastAutoGenerator.create(DB_URL, DB_USERNAME, DB_PASSWORD)
                .globalConfig(builder -> {
                    builder.author(System.getProperty("user.name")) // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(javaPath); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig((scanner, builder) -> {

                    String moduleName = StringUtils.defaultString(scanner.apply("请输入模块名："));
                    builder.parent(packageName) // 设置父包名
                            .moduleName(moduleName) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, xmlPath + moduleName)); // 设置mapperXml生成路径
                }).strategyConfig((scanner, builder) -> {
                    // builder.addInclude("t_simple") // 设置需要生成的表名
                    //         .addTablePrefix("t_", "c_"); // 设置过滤表前缀
                    builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                            .controllerBuilder()
                            .enableRestStyle()
                            .enableHyphenStyle()
                            .entityBuilder()
                            .enableLombok()
                            .addTableFills(
                                    new Column("create_time", FieldFill.INSERT),
                                    new Column("create_by", FieldFill.INSERT),
                                    new Column("update_time", FieldFill.UPDATE),
                                    new Column("update_by", FieldFill.UPDATE)
                            ).logicDeleteColumnName("del_flag")
                            .build();
                })
                //.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
